<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cities_model extends CI_Model {

	public function __construct()
	{
		$this->load->database();
                date_default_timezone_set('Europe/Skopje');
	}
        
        public function list_sities()
        {
            $query = $this->db->get('cities');
            return $query->result_array();            
        }    
        
        public function add_city()
        {
                $this->load->helper('url');

                if(array_key_exists('is_active', $this->input->post()))
                {
                    $is_active = 1;
                }
                else 
                {
                    $is_active = 0;     
                }                
                
                $date_added = date('Y-m-d H:i:s', time());                
                
                $data = array(
                        'name' => $this->input->post('name'),
                        'zip' => $this->input->post('zip'),
                        'lng' => $this->input->post('lng'),
                        'lat' => $this->input->post('lat'),
                        'is_active' => $is_active,
                        'date_added' => $date_added
                );

                return $this->db->insert('cities', $data);
        }        
        
        public function get_row( $city_id )
        {
            $query = $this->db->get_where('cities',  array('city_id' => $city_id));            
            return $query->row_array();            
        }     
        
        public function edit_city(  )
        {
                $this->load->helper('url');

                if(array_key_exists('is_active', $this->input->post()))
                {
                    $is_active = 1;
                }
                else 
                {
                    $is_active = 0;     
                }                                              
                
                $city_id = $this->input->post('city_id');
                
                $data = array(
                        'name' => $this->input->post('name'),
                        'zip' => $this->input->post('zip'),
                        'lng' => $this->input->post('lng'),
                        'lat' => $this->input->post('lat'),
                        'is_active' => $is_active,
                );
                
                
                $this->db->where('city_id', $city_id);
                return $this->db->update('cities', $data); 
        }         
        
        public function delete_row( $city_id )
        {       
                $this->db->where('city_id', $city_id);
                return $this->db->delete('cities'); 
        }           
}

?>
